using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectKhachHang()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_KHACHHANG
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectKhachHangPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_KHACHHANG,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListKhachHang", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteKhachHang(int id)
		{
			var deleteItem = _repKhachHang.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var result = _repKhachHang.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                    FileHelper.DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repKhachHang.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfKhachHang(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_KHACHHANG,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateKhachHang(FormCollection forms)
		{
			string id = forms.GetValues("KhachHang_ID")[0];
			string tencongty = forms.GetValues("KhachHang_TenCongTy")[0];
			string url = forms.GetValues("KhachHang_URL")[0];
			var file = Request.Files["KhachHang_HinhMinhHoa"];
            var fileslideshow = Request.Files["KhachHang_HinhSlideShow"];

			string refName = "";
			var editItem = _repKhachHang.SelectByID(int.Parse(id));
			editItem.TenCongTy = tencongty;
			editItem.URL = url;
			var result = false;

			if (file.ContentLength != 0)
			{
                result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), id, editItem.HinhMinhHoa, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
				editItem.HinhMinhHoa = refName;
			}
            if (fileslideshow.ContentLength != 0)
            {
                result = FileHelper.UploadFile(fileslideshow, Server.MapPath("/Content/Images/Items/"), id, editItem.ID.ToString(), WebConfiguration.MediumImageWidth, WebConfiguration.MediumImageHeight, ref refName);
                editItem.HinhSlideShow = refName;
            }

			result = _repKhachHang.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_KHACHHANG,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfKhachHang", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertKhachHang(FormCollection forms)
		{
			string tencongty = forms.GetValues("KhachHang_TenCongTy")[0];
			string url = forms.GetValues("KhachHang_URL")[0];
			var file = Request.Files["KhachHang_HinhMinhHoa"];
            var fileslideshow = Request.Files["KhachHang_HinhSlideShow"];

			string refName = "";
			var newItem = new KhachHang
			{
				TenCongTy = tencongty,
				URL = url,
				HinhMinhHoa = "noimages.jpg",
                HinhSlideShow = "noimages.jpg"
			};
			var result = _repKhachHang.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
                    result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), newItem.ID.ToString(), string.Empty, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
					if (result)
					{
                        var addedItem = _repKhachHang.SelectByID(newItem.ID);
                        addedItem.HinhMinhHoa = refName;
                        _repKhachHang.Save();
					}
				}

                if (fileslideshow.ContentLength != 0)
                {
                    result = FileHelper.UploadFile(fileslideshow, Server.MapPath("/Content/Images/Items/"), newItem.ID.ToString(), string.Empty, WebConfiguration.MediumImageWidth, WebConfiguration.MediumImageHeight, ref refName);
                    if (result)
                    {
                        var addedItem = _repKhachHang.SelectByID(newItem.ID);
                        addedItem.HinhSlideShow = refName;
                        _repKhachHang.Save();
                    }
                }
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_KHACHHANG,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectKhachHang", CreateViewModel(data));
		}


	}
}